package forms;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;

public class DrzaveForm extends JDialog {
	private static final long serialVersionUID = 1L;
	JPanel topPanel;
	JPanel bottomPanel;
	JPanel middlePanel;
	JTable table;
	JToolBar toolBar;
	JTextField textField1, textField2;
	JButton commitBtn, button2, searchBtn, addBtn, editBtn, refreshBtn,
			prevFormBtn;
	JButton helpBtn, firstBtn, prevBtn, nextBtn, lastBtn, removeBtn,
			nextFormBtn;
	JLabel label1, label2;
	private static final int MODE_DEFAULT = 0;
	private static final int MODE_EDIT = 1;
	private static final int MODE_ADD = 2;
	private static final int MODE_SEARCH = 3;
	private static final int MODE_DELETE = 4;
	private int mode;

	private void initPanels() {
		topPanel = new JPanel();
		middlePanel = new JPanel();
		bottomPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		middlePanel.setLayout(new BorderLayout());
		bottomPanel
				.setLayout(new MigLayout("", "[align r][grow,fill][align l]"));
	}

	private void initTable() {
		table = new JTable();

		// Omogućavanje skrolovanja ubacivanjem tabele u ScrollPane
		JScrollPane scrollPane = new JScrollPane(table);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		middlePanel.add(scrollPane, BorderLayout.CENTER);

		// Kreiranje TableModel-a, parametri: header-i kolona i broj redova
		DrzaveTableModel tableModel = new DrzaveTableModel(new String[] {
				"Šifra", "Naziv" }, 0);
		table.setModel(tableModel);

		try {
			tableModel.open();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// Dozvoljeno selektovanje redova
		table.setRowSelectionAllowed(true);
		// Ali ne i selektovanje kolona
		table.setColumnSelectionAllowed(false);

		// Dozvoljeno selektovanje samo jednog reda u jedinici vremena
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting())
							return;
						sync();
					}
				});

	}

	private void initToolbar() {
		toolBar = new JToolBar();
		ImageIcon searchIcon = new ImageIcon("images/search.gif");
		ImageIcon addIcon = new ImageIcon("images/add.gif");
		ImageIcon editIcon = new ImageIcon("images/edit.gif");
		ImageIcon refreshBtn1Icon = new ImageIcon("images/refresh.gif");
		ImageIcon prevFormIcon = new ImageIcon("images/zoom-pickup.gif");
		ImageIcon helpIcon = new ImageIcon("images/help.gif");
		ImageIcon firstIcon = new ImageIcon("images/first.gif");
		ImageIcon nextIcon = new ImageIcon("images/next.gif");
		ImageIcon prevIcon = new ImageIcon("images/prev.gif");
		ImageIcon lastIcon = new ImageIcon("images/last.gif");
		ImageIcon removeIcon = new ImageIcon("images/remove.gif");
		ImageIcon nextFormIcon = new ImageIcon("images/nextform.gif");
		refreshBtn = new JButton(refreshBtn1Icon);
		refreshBtn.setToolTipText("Osvežavanje prikaza");
		searchBtn = new JButton(searchIcon);
		searchBtn.setToolTipText("Pretraga");
		addBtn = new JButton(addIcon);
		addBtn.setToolTipText("Unos");
		editBtn = new JButton(editIcon);
		editBtn.setToolTipText("Azuriranje podataka");
		prevFormBtn = new JButton(prevFormIcon);
		prevFormBtn.setToolTipText("Povratak na prethodnu formu");
		helpBtn = new JButton(helpIcon);
		helpBtn.setToolTipText("Pomoć");
		firstBtn = new JButton(firstIcon);
		firstBtn.setToolTipText("Prvi slog");
		prevBtn = new JButton(prevIcon);
		prevBtn.setToolTipText("Prethodni slog");
		nextBtn = new JButton(nextIcon);
		nextBtn.setToolTipText("Sledeći slog");
		lastBtn = new JButton(lastIcon);
		lastBtn.setToolTipText("Poslednji slog");
		removeBtn = new JButton(removeIcon);
		removeBtn.setToolTipText("Brisanje");
		nextFormBtn = new JButton(nextFormIcon);
		nextFormBtn.setToolTipText("Prelazak na sledeću formu");
		toolBar.add(searchBtn);
		toolBar.add(refreshBtn);
		toolBar.add(prevFormBtn);
		toolBar.add(helpBtn);
		toolBar.addSeparator();
		toolBar.add(firstBtn);
		toolBar.add(prevBtn);
		toolBar.add(nextBtn);
		toolBar.add(lastBtn);
		toolBar.addSeparator();
		toolBar.add(addBtn);
		toolBar.add(removeBtn);
		toolBar.addSeparator();
		toolBar.add(nextFormBtn);
		topPanel.add(toolBar);
	}

	private void initBottomPanel() {
		ImageIcon removeIcon = new ImageIcon("images/remove.gif");
		ImageIcon commitIcon = new ImageIcon("images/commit.gif");
		textField1 = new JTextField();
		textField2 = new JTextField();
		commitBtn = new JButton(commitIcon);
		button2 = new JButton(removeIcon);
		label1 = new JLabel("Sifra drzave:");
		label2 = new JLabel("Naziv drzave:");
		bottomPanel.add(label1);
		bottomPanel.add(textField1);
		bottomPanel.add(commitBtn, "wrap");
		bottomPanel.add(label2);
		bottomPanel.add(textField2);
		bottomPanel.add(button2);
	}

	private void goLast() {
		int rowCount = table.getModel().getRowCount();
		if (rowCount > 0) {
			table.setRowSelectionInterval(rowCount - 1, rowCount - 1);
			sync();
		}
	}

	private void goFirst() {
		int rowCount = table.getModel().getRowCount();
		if (rowCount > 0) {
			table.setRowSelectionInterval(0, 0);
			sync();
		}
	}

	private void goNext() {
		int rowCount = table.getModel().getRowCount();
		if (rowCount > 0) {
			int selRow = table.getSelectedRow();
			if (selRow < (rowCount - 1)) {
				table.setRowSelectionInterval(selRow + 1, selRow + 1);
			}
			sync();
		}
	}

	private void goPrev() {
		int rowCount = table.getModel().getRowCount();
		if (rowCount > 0) {
			int selRow = table.getSelectedRow();
			if (selRow > 0) {
				table.setRowSelectionInterval(selRow - 1, selRow - 1);
			}
			sync();
		}
	}

	private void sync() {
		int index = table.getSelectedRow();
		if (index < 0) {
			textField1.setText("");
			textField2.setText("");
			return;
		}
		String sifra = (String) table.getModel().getValueAt(index, 0);
		String naziv = (String) table.getModel().getValueAt(index, 1);
		textField1.setText(sifra);
		textField2.setText(naziv);
	}

	private void removeRow() {
		int index = table.getSelectedRow();
		if (index == -1) // Ako nema selektovanog reda (tabela prazna)
			return; // izlazak
		// kada obrisemo tekuci red, selektovacemo sledeci (newindex):
		int newIndex = index;
		// sem ako se obrise poslednji red, tada selektujemo prethodni
		if (index == table.getModel().getRowCount() - 1)
			newIndex--;
		try {
			DrzaveTableModel dtm = (DrzaveTableModel) table.getModel();
			dtm.deleteRow(index);
			if (table.getModel().getRowCount() > 0)
				table.setRowSelectionInterval(newIndex, newIndex);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void addRow() {
		String sifra = textField1.getText().trim();
		String naziv = textField2.getText().trim();
		try {
			DrzaveTableModel dtm = (DrzaveTableModel) table.getModel();
			int index = dtm.insertRow(sifra, naziv);
			table.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void setMode(int m) {
		mode = m;
		if(mode==DrzaveForm.MODE_DELETE) {
			searchBtn.setEnabled(false);
			editBtn.setEnabled(false);
			addBtn.setEnabled(false);
			return;
		}
		if(mode==DrzaveForm.MODE_ADD) {
			removeBtn.setEnabled(false);
			searchBtn.setEnabled(false);
			editBtn.setEnabled(false);
			return;
		}
		if(mode==DrzaveForm.MODE_EDIT) {
			searchBtn.setEnabled(false);
			removeBtn.setEnabled(false);
			addBtn.setEnabled(false);
			return;
		}
		if(mode==DrzaveForm.MODE_SEARCH) {
			removeBtn.setEnabled(false);
			addBtn.setEnabled(false);
			editBtn.setEnabled(false);
			return;
		}
		if(mode==DrzaveForm.MODE_DEFAULT) {
			removeBtn.setEnabled(true);
			addBtn.setEnabled(true);
			editBtn.setEnabled(true);
			searchBtn.setEnabled(true);
			return;
		}
	}

	public DrzaveForm() {
		mode = DrzaveForm.MODE_SEARCH;
		setTitle("Drzave");
		initPanels();
		initToolbar();
		initTable();
		initBottomPanel();
		setLayout(new BorderLayout());
		add(topPanel, BorderLayout.NORTH);
		add(middlePanel, BorderLayout.CENTER);
		add(bottomPanel, BorderLayout.SOUTH);
		setSize((int) (MainForm.getInstance().getSize().getWidth() * 0.666f),
				(int) (MainForm.getInstance().getSize().getHeight() * 0.666f));
		setLocationRelativeTo(MainForm.getInstance());
		lastBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				goLast();
			}
		});
		firstBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				goFirst();
			}
		});
		nextBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				goNext();
			}
		});
		prevBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				goPrev();
			}
		});
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setMode(DrzaveForm.MODE_DEFAULT);
			}
		});
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setMode(MODE_DELETE);
			}
		});
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setMode(MODE_ADD);
				textField1.requestFocus();
			}
		});
		commitBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (mode==MODE_DELETE) {
					removeRow();
				}
				if(mode==MODE_ADD) {
					addRow();
					textField1.setText("");
					textField2.setText("");
				}
			}
		});
	}
}
